/**
 * Created by FreezeSoul on 2018/10/25.
 */
define([
    "app/core/base",
    "app/core/util",
    "../../controls/common/commonMeta",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel',
    'jqueryTmpl',
    'css!app/plugins/project/executeInfo/resources/style.css'
], function (Chart, Util, Meta, $, _, JSON, log) {

    var property = {
        config: "property",
        option: [
            {
                group: '绑定',
                id: 'bingdingData',
                name: '绑定值数据源',
                enable: true,
                type: 'sortBinding',
                dataset: '',
                columns: [],
                tooltip: '绑定值数据源，依次为实际生产量、实际库存量、在途运输量、堆场库存量、实际到货量、大部件类型'
            }
        ].concat($.extend(true, [], Meta.commonProperties)),
        MapOption: function (option) {
            Util.linkOption(this.option, option);
        }
    };

    var chart = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._container = null;
            this._template = '<div class="execute_info_container scroll-style-3"><div class="row1"></div><div class="row2"></div></div>';
            this._templateI = '<div class="execute_info_head_item"><div class="execute_info_title">${key}</div><div class="execute_info_value">${value}</div></div>';
            this._templateII = '<div class="execute_info_row_item">' +
                '<div class="execute_info_title">${key}</div>' +
                '<div class="execute_info_value">${value1}</div>' +
                '<div class="execute_info_value">${value2}</div>' +
                '<div class="execute_info_value">${value3}</div>' +
                '<div class="execute_info_value">${value4}</div>' +
                '<div class="execute_info_value">${value5}</div>' +
                '</div>';
            this._element = null;
            this._option = {
                Series: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {
                    uuid: '',
                    class: '',
                    display: true,
                    dataI: [
                        {key: "实际生产量", value: "56%"},
                        {key: "实际库存量", value: "56%"},
                        {key: "在途运输量", value: "56%"},
                        {key: "堆场库存量", value: "56%"},
                        {key: "实际到货量", value: "56%"}
                    ],
                    dataII: [
                        {key: "机舱", value: [125, 125, 125, 125, 125]},
                        {key: "叶轮", value: [125, 125, 125, 125, 125]},
                        {key: "叶片", value: [125, 125, 125, 125, 125]},
                        {key: "发电机", value: [125, 125, 125, 125, 125]}
                    ]
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _render: function (option) {
            $(this._container).empty();
            var dataI = $.extend(true, [], option.dataI);
            var dataII = $.extend(true, [], option.dataII);
            _.each(dataII, function (obj) {
                obj.value1 = obj.value[0];
                obj.value2 = obj.value[1];
                obj.value3 = obj.value[2];
                obj.value4 = obj.value[3];
                obj.value5 = obj.value[4];
            });
            var $row1 = $.tmpl(this._templateI, dataI);
            var $row2 = $.tmpl(this._templateII, dataII);

            var $container = $(this._template);
            $row1.appendTo($container.find(".row1"));
            $row2.appendTo($container.find(".row2"));
            $container.appendTo(this._container);

            $container.find(".execute_info_head_item .execute_info_value").hover(
                function () {
                    if ($(this).hasClass("animated")) return;
                    $(this).addClass("animated flash")
                        .one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
                            $(this).removeClass("animated flash")
                        });
                }, function () {
                }
            );

            return $container;
        },
        example: function () {
            try {

                var exampleOption = {
                    uuid: '',
                    class: '',
                    display: true,
                    data: []
                };

                this._element = this._render(exampleOption);
            } catch (e) {
                log.error(e);
            }
        },
        _bindData: function (data) {
            var bindProperty = _.findWhere(this._option.Extend.option, {id: 'bingdingData'});
            if (!bindProperty || !bindProperty.enable) return;

            var dataSetCode = bindProperty.dataset;
            var columns = bindProperty.columns;

            var checkNum = function (num) {
                if (num === null) num = 0;
                return num;
            };

            var makeArray = function (row) {
                return [
                    checkNum(row[columns[0].ColumnName]),
                    checkNum(row[columns[1].ColumnName]),
                    checkNum(row[columns[2].ColumnName]),
                    checkNum(row[columns[3].ColumnName]),
                    checkNum(row[columns[4].ColumnName])
                ]
            };

            var makeArray2 = function (jiCangRow, yeLunRow, yePianRow, faDianJiRow, index) {
                return checkNum(jiCangRow[columns[index].ColumnName]) +
                    checkNum(yeLunRow[columns[index].ColumnName]) +
                    checkNum(yePianRow[columns[index].ColumnName]) +
                    checkNum(faDianJiRow[columns[index].ColumnName]);
            };

            var table = data[dataSetCode];
            if (table.length === 4) {

                var jiCangRow = _.filter(table, function (row) {
                    return row[columns[5].ColumnName] === "机舱";
                })[0];
                var yeLunRow = _.filter(table, function (row) {
                    return row[columns[5].ColumnName] === "叶轮";
                })[0];
                var yePianRow = _.filter(table, function (row) {
                    return row[columns[5].ColumnName] === "叶片";
                })[0];
                var faDianJiRow = _.filter(table, function (row) {
                    return row[columns[5].ColumnName] === "发电机";
                })[0];

                this._option.Option.dataI = [];
                this._option.Option.dataII = [];

                this._option.Option.dataII.push({
                    key: "机舱", value: makeArray(jiCangRow)
                });
                this._option.Option.dataII.push({
                    key: "叶轮", value: makeArray(yeLunRow)
                });
                this._option.Option.dataII.push({
                    key: "叶片", value: makeArray(yePianRow)
                });
                this._option.Option.dataII.push({
                    key: "发电机", value: makeArray(faDianJiRow)
                });
                this._option.Option.dataI.push({
                    key: "实际生产量",
                    value: makeArray2(jiCangRow, yeLunRow, yePianRow, faDianJiRow, 0)
                });
                this._option.Option.dataI.push({
                    key: "实际库存量",
                    value: makeArray2(jiCangRow, yeLunRow, yePianRow, faDianJiRow, 1)
                });
                this._option.Option.dataI.push({
                    key: "在途运输量",
                    value: makeArray2(jiCangRow, yeLunRow, yePianRow, faDianJiRow, 2)
                });
                this._option.Option.dataI.push({
                    key: "堆场库存量",
                    value: makeArray2(jiCangRow, yeLunRow, yePianRow, faDianJiRow, 3)
                });
                this._option.Option.dataI.push({
                    key: "实际到货量",
                    value: makeArray2(jiCangRow, yeLunRow, yePianRow, faDianJiRow, 4)
                });
            }

        },
        setData: function (data) {
            try {
                this._readProperty();

                if (data
                    && this._option.Binding
                    && this._option.Binding.length > 0
                    && this._option.Extend
                ) {
                    this._bindData(data);
                }
                this._element = this._render(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                $(this._element).off().remove();
            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._element;
        },
        getChart: function () {
            return this._element;
        }
    });

    return {
        Chart: chart,
        Property: property
    };
});